﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CognisessService;
using CognisessWeb.Models;
using CognisessDA;

namespace CognisessWeb.Controllers
{
    public class BlogController : Controller
    {
        //
        // GET: /Blog/
        AccountBLL accountBLL = new AccountBLL();

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit(int id)
        {
            List<BlogsModel> listBlog = new List<BlogsModel>();
            List<Blog> blogs = accountBLL.GetBlog(new Blog { Id = id });
            // 每一个view只会绑定一个blog.
            //Blog blog = blogs.FirstOrDefault(); // 需要绑定上去的blog
            List<Post> posts = new List<Post>();
            BlogsModel bindBlog = new BlogsModel();
            foreach (var item in blogs)
            {
                bindBlog.Id = item.Id;
                bindBlog.BloggerName = item.BloggerName;
                bindBlog.Title = item.Title;
                bindBlog.BlogContent = item.BlogContent;
                bindBlog.CreateTime = DateTime.Parse(item.CreateTime.ToString());
                bindBlog.Posts = new List<PostsModel> { };
                posts = accountBLL.GetPost(new Post { BlogId = bindBlog.Id });
                // 根据blog id查询出该blog下面所有的post.
                for (int i = 0; i < posts.Count; i++)
                {
                    List<CommentsModel> commentsModel = new List<CommentsModel>();
                    List<Comment> comments = accountBLL.GetComment(new Comment { PostId = posts[i].Id });
                    // 根据post id查询出每个post下面的comments
                    for (int j = 0; j < comments.Count; j++)
                    {
                        commentsModel.Add(new CommentsModel { Id = comments[j].Id, Content = comments[j].CommentContent, DateCreated = DateTime.Parse(comments[j].DateCreated.ToString()), PostId = posts[i].Id, Post = new PostsModel { Id = posts[i].Id, Title = posts[i].Title, Content = posts[i].PostContent, BlogId = bindBlog.Id, Comments = commentsModel } });
                    }
                    bindBlog.Posts.Add(new PostsModel { BlogId = bindBlog.Id, Content = posts[i].PostContent, Id = posts[i].Id, Title = posts[i].Title, DateCreated = DateTime.Parse(posts[i].dateCreated.ToString()), Comments = commentsModel });
                }
            }
            return View(bindBlog);
        }

        public ActionResult Index()
        {
            List<BlogsModel> listBlog = new List<BlogsModel>();
            List<Blog> blogs = accountBLL.GetBlog(new Blog());
            // 每一个view只会绑定一个blog.
            //Blog blog = blogs.FirstOrDefault(); // 需要绑定上去的blog
            List<Post> posts = new List<Post>();
            foreach (var item in blogs)
            {
                BlogsModel bindBlog = new BlogsModel();
                bindBlog.Id = item.Id;
                bindBlog.BloggerName = item.BloggerName;
                bindBlog.Title = item.Title;
                bindBlog.BlogContent = item.BlogContent;
                bindBlog.CreateTime = DateTime.Parse(item.CreateTime.ToString());
                bindBlog.Posts = new List<PostsModel> { };
                posts = accountBLL.GetPost(new Post { BlogId = bindBlog.Id });
                // 根据blog id查询出该blog下面所有的post.
                for (int i = 0; i < posts.Count; i++)
                {
                    List<CommentsModel> commentsModel = new List<CommentsModel>();
                    List<Comment> comments = accountBLL.GetComment(new Comment { PostId = posts[i].Id });
                    // 根据post id查询出每个post下面的comments
                    for (int j = 0; j < comments.Count; j++)
                    {
                        commentsModel.Add(new CommentsModel { Id = comments[j].Id, Content = comments[j].CommentContent, DateCreated = DateTime.Parse(comments[j].DateCreated.ToString()), PostId = posts[i].Id, Post = new PostsModel { Id = posts[i].Id, Title = posts[i].Title, Content = posts[i].PostContent, BlogId = bindBlog.Id, Comments = commentsModel } });
                    }
                    bindBlog.Posts.Add(new PostsModel { BlogId = bindBlog.Id, Content = posts[i].PostContent, Id = posts[i].Id, Title = posts[i].Title, DateCreated = DateTime.Parse(posts[i].dateCreated.ToString()), Comments = commentsModel });
                }
                listBlog.Add(bindBlog);
            }

            //if (page == null)
            //    page = 0;
            //const int pageSize = 2;
            //var list = listBlog.Skip((page ?? 0) * pageSize).Take(pageSize);
            return View(listBlog);
        }

       
        public ActionResult Details(int Id)
        {
            List<BlogsModel> listBlog = new List<BlogsModel>();
            List<Blog> blogs = accountBLL.GetBlog(new Blog { Id = Id });
            // 每一个view只会绑定一个blog.
            //Blog blog = blogs.FirstOrDefault(); // 需要绑定上去的blog
            List<Post> posts = new List<Post>();
            BlogsModel bindBlog = new BlogsModel();
            foreach (var item in blogs)
            {                
                bindBlog.Id = item.Id;
                bindBlog.BloggerName = item.BloggerName;
                bindBlog.Title = item.Title;
                bindBlog.BlogContent = item.BlogContent;
                bindBlog.CreateTime = DateTime.Parse(item.CreateTime.ToString());
                bindBlog.Posts = new List<PostsModel> { };
                posts = accountBLL.GetPost(new Post { BlogId = bindBlog.Id });
                // 根据blog id查询出该blog下面所有的post.
                for (int i = 0; i < posts.Count; i++)
                {
                    List<CommentsModel> commentsModel = new List<CommentsModel>();
                    List<Comment> comments = accountBLL.GetComment(new Comment { PostId = posts[i].Id });
                    // 根据post id查询出每个post下面的comments
                    for (int j = 0; j < comments.Count; j++)
                    {
                        commentsModel.Add(new CommentsModel { Id = comments[j].Id, Content = comments[j].CommentContent, DateCreated = DateTime.Parse(comments[j].DateCreated.ToString()), PostId = posts[i].Id, Post = new PostsModel { Id = posts[i].Id, Title = posts[i].Title, Content = posts[i].PostContent, BlogId = bindBlog.Id, Comments = commentsModel } });
                    }
                    bindBlog.Posts.Add(new PostsModel { BlogId = bindBlog.Id, Content = posts[i].PostContent, Id = posts[i].Id, Title = posts[i].Title, DateCreated = DateTime.Parse(posts[i].dateCreated.ToString()), Comments = commentsModel });
                }
            }
            return View(bindBlog);
        }


        public ActionResult Delete(int id)
        {
            bool isSuccessful = accountBLL.DeleteBlog(id);
            if (isSuccessful)
            {
                TempData["ExcuteSuccfull"] = "Delete Successfully.";
            }
            else
            {
                TempData["ExcuteSuccfull"] = "Operation fail. Please contact with the administrator.";
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Edit(Blog blog)
        {
            List<Blog> blogs = new List<Blog>();
            blog.CreateTime = DateTime.Now;
            blogs.Add(blog);
            accountBLL.UpdataBlog(blogs);
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Create(Blog blog)
        {
            try
            {
                List<Blog> blogs = new List<Blog>();
                blogs.Add(blog);
                accountBLL.AddBlog(blogs);
                return RedirectToAction("Index");
            }
            catch (Exception)
            {

                return View();
            }

        }

    }
}
